package com.crawleService.shanxitobaccoWebsite.service.impl;

import com.crawleService.shanxitobaccoWebsite.service.MerchantRetailInteractionService;

import com.crawleService.util.shanxitobaccoUtil.SUtil;
import com.projectCommon.bean.Info;
import com.projectCommon.bean.httpSmokeBean.shanxitobaccobean.ThreePublishRes;
import com.projectCommon.bean.httpSmokeBean.shanxitobaccobean.request.SmokeInfoBean;
import com.projectCommon.util.commonUtil.CUtil;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Objects;


@Service
public class MerchantRetailInteractionServiceImpl implements MerchantRetailInteractionService {
    //三公开点击更多
    @Override
//    https://sx.jinye.cn/wdk?action=ecw.page&method=call_service&ajaxparamtime=1735352764811
//    https://sx.jinye.cn/wdk?action=ecw.page&method=call_service&ajaxparamtime=1735353092902
    public ThreePublishRes threeOpenness_more() {
        OkHttpClient client = new OkHttpClient();
        String url = "https://sx.jinye.cn/wdk?action=ecw.page&method=call_service&ajaxparamtime=" + System.currentTimeMillis();
        String wppm = SUtil.encipher(CUtil.obgToJson(new SmokeInfoBean().init7()));
        FormBody formBody = new FormBody.Builder()
                .add("wppm", wppm)
                .build();
        Request.Builder builder = new Request.Builder();
        builder = CUtil.setCookie(builder, Info.ShanxiTobaccoCookieMap);
        Request request = builder
                .url(url)
                .post(formBody)
                .build();
        try {
            Response response = client.newCall(request).execute();
            String string = response.body().string();
            ThreePublishRes threePublishRes = CUtil.jsonToObj(Objects.requireNonNull(string), ThreePublishRes.class);
            CUtil.set_SetCookie(response.headers(), Info.ShanxiTobaccoCookieMap);
            return threePublishRes;
        } catch (IOException e) {
            return null;
        }
    }

    @Override
    public void viewPublicInformation() {

    }

    @Override
    public byte[] downPublicInformation(String supplementaryWebsite) {
        OkHttpClient client = new OkHttpClient();
        String url = "https://sx.jinye.cn" + supplementaryWebsite;
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = client.newCall(request).execute()) {
            if (!response.isSuccessful()) {
                throw new IOException("Unexpected code " + response);
            }
            // 获取响应体
            byte[] fileBytes = response.body().bytes();
            return fileBytes;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
